package com.server.controller;



import com.common.context.BaseContext;
import com.common.result.Result;
import com.pojo.dto.ArticleDTO;
import com.pojo.entity.Article;
import com.pojo.vo.ArticleVO;
import com.server.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/article")
@Slf4j
@Api(tags = "文章接口")
public class ArticleController {
    /*
    * 获取文章
    * 前端无数据发送
    * 返回List<ArticleVO>集合
    **/

    @Autowired
    private ArticleService articleService;

    @PostMapping("/getArticlesList")
    @ApiOperation("获取文章列表")
    public Result<List<ArticleVO>> GetArticleList(){
            List<Article> articleList = articleService.GetArticleList();

            List<ArticleVO> articleVOS = articleList.stream()
                    .map(article -> ArticleVO.builder()
                            .id(article.getId())
                            .title(article.getTitle())
                            .content(article.getContent())
                            .userId(article.getUserId())
                            .createDate(article.getCreateDate())
                            .updateTime(article.getUpdateTime())
                            .build())
                    .collect(Collectors.toList());
            return Result.success(articleVOS);
    }

    @PostMapping("/getArticleById")
    @ApiOperation("根据id获取文章")
    public Result<ArticleVO> GetArticleById(Integer id){
        Article article = articleService.GetArticleById(id);

       if (article!=null){
           ArticleVO articleVO = ArticleVO.builder()
                   .id(article.getId())
                   .title(article.getTitle())
                   .content(article.getContent())
                   .userId(article.getUserId())
                   .createDate(article.getCreateDate())
                   .updateTime(article.getUpdateTime())
                   .build();

           return Result.success(articleVO);
       }else {
           return Result.error("文章不存在");
       }
    }

    @PostMapping("/addArticle")
    @ApiOperation("添加文章")
    public Result addArticle(@RequestBody ArticleDTO articleDTO){
        log.info("添加文章:{}",articleDTO);
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO,article);
        article.setCreateDate(LocalDate.now());
        article.setUserId(BaseContext.getCurrentId());
        article.setUpdateTime(LocalDate.now());
        articleService.insetArticle(article);
        return Result.success();
    }

    @GetMapping("/search")
    @ApiOperation("根据标题搜索文章")
    public Result<List<Article>> searchArticles(@RequestParam("title") String title){

        return Result.success(articleService.selectArticleByTitle(title));
    }

    @GetMapping("/getUserArticle")
    @ApiOperation("根据用户id获取文章")
    public Result<List<Article>> getByUserId(){
        Integer userId = BaseContext.getCurrentId();
        return Result.success(articleService.selectArticleByUserId(userId));
    }

    @DeleteMapping("/delArticle")
    @ApiOperation(("根据id删除文章"))
    public Result delArticleById(@RequestParam("id") Integer id){
        articleService.delById(id);
        return Result.success();
    }

}
